//
//  CustomMenuButton.m
//  Exam
//
//  Created by mll on 14-6-7.
//  Copyright (c) 2014年 Changzhou Jinzhineng Software Co., Ltd. All rights reserved.
//

#import "CustomMenuButton.h"
#import "UIColor+JZNExtension.h"
#import "CommonMacro.h"
#import "HZActivityIndicatorView.h"
#import "UIImage+JZNExtension.h"
#import <Masonry.h>

@implementation CustomMenuButton
{
    UIView *actIndViewBg;
    HZActivityIndicatorView *actIndView;
}

@synthesize buttonType;
-(id) init{
    self = [super init];
    if (!self) {
        // Initialization code
        return nil;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

// 设置按钮信息:title:icon:index
- (void)setButtonInfo:(NSDictionary*)btnInfo;
{
        //设置bgImage
        [self setBackgroundImage:[UIImage imageNamed:@"bottomButton_bg"] forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"bottomButtonhlight_bg"] forState:UIControlStateHighlighted];
        [self setBackgroundImage:[UIImage imageNamed:@"bottomButtonhlight_bg"] forState:UIControlStateSelected];
        NSString* title = [btnInfo objectForKey:@"title"];
        NSString* icon = [btnInfo objectForKey:@"icon"];
        NSString* highlightIcon = [btnInfo objectForKey:@"highlightIcon"];
        //    NSInteger menuItem = [btnInfo intForKey:@"index"];
        [self setTitle:title forState:UIControlStateNormal];
        CGFloat titleFontSize = 14.0f;
        if (buttonType == ButtonTypeMarkerPopup) {
            titleFontSize = 12.0f;
        }
        [self.titleLabel setFont:[UIFont fontWithName:kDefaultFontName size:titleFontSize]];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self setTitleColor:[UIColor colorWithHex:0x666666] forState:UIControlStateNormal];
//        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        if (buttonType == ButtonTypeMarkerPopup) {
            [self setImage:[[UIImage imageNamed:icon] imageReSize:CGSizeMake(32, 32)] forState:UIControlStateNormal];
            [self setImage:[[UIImage imageNamed:highlightIcon] imageReSize:CGSizeMake(32, 32)] forState:UIControlStateHighlighted];
        }
        else {
            [self setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
            [self setImage:[UIImage imageNamed:highlightIcon] forState:UIControlStateHighlighted];
        }
        
        
        
        self.adjustsImageWhenHighlighted = NO;
        self.adjustsImageWhenDisabled = NO;
        
        
        // the space between the image and text
        CGFloat spacing = 4.0;
        
        // get the size of the elements here for readability
        
        CGSize imageSize = self.imageView.frame.size;
        CGSize titleSize = self.titleLabel.frame.size;
        
        // lower the text and push it left to center it
        self.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(imageSize.height + spacing), spacing);
        
        // the text width might have changed (in case it was shortened before due to
        // lack of space and isn't anymore now), so we get the frame size again
        titleSize = self.titleLabel.frame.size;
    
    
        // raise the image and push it right to center it
        self.imageEdgeInsets = UIEdgeInsetsMake(-(imageSize.height + spacing), 0, 0, -imageSize.width);
    
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.titleLabel);
        }];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

- (void)setHighlighted:(BOOL)highlighted {
    if (self.highlighted != highlighted) {
        [super setHighlighted:highlighted];
        [self setNeedsDisplay];
    }
}

- (void)setSelected:(BOOL)selected
{
    if (self.selected != selected) {
        [super setSelected:selected];
        [self setNeedsDisplay];
        [self layoutSubviews];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
@end
